<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

require_once(APPPATH.'controllers/admin/applications.php');
class Request extends Applications
{
	public function __construct()
	{
		parent::__construct();

		/**
		 * @description check the session variables
		 */
		$this->M_login->session_check();

		/**
		 * @description the URL
		 */
		$this->bz_segment['END'] = 3;
		$this->bz_segment['URL'] = $this->M_functions->segment_create();
		$this->bz_segment['SITE_URL'] = site_url($this->bz_segment['URL']);

		/**
		 * @description access rights
		 */
		$this->access_rights = $this->M_menu->valid_check('db_default', $this->session->userdata('session_user_id'));
		$this->access_rights = array
			(
				'ADD' => in_array('add', $this->access_rights) ? TRUE : FALSE
				, 'EDIT' => in_array('edit', $this->access_rights) ? TRUE : FALSE
				, 'DELETE' => FALSE
			);

		/**
		 * @description initialize variables for database
		 */
		$this->tbl_attr = array
			(
				'TABLE' => array
					(
						'NAME' => 'hris_employee_data'
						, 'NAME_ALIAS' => 'hris_employee_data a'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'TABLE_MODAL' => array
					(
						'NAME' => 'hris_attendance_request'
						, 'NAME_ALIAS' => 'hris_attendance_request b'
						, 'PRIMARY' => array
							(
								'SHOW' => FALSE
								, 'DATA' => array('id')
							)
					)
				, 'VIEW_DETAIL' => array
					(
						'HEADING' => array
							(
								$this->lang->line('hris_type')
								, $this->lang->line('hris_number')
								, $this->lang->line('hris_attendance_request') . ' ' . $this->lang->line('hris_date')
								, $this->lang->line('hris_from')
								, $this->lang->line('hris_to')
								, $this->lang->line('hris_duration')
								, $this->lang->line('hris_attendance_approval') . ' ' . $this->lang->line('hris_status')
								, $this->lang->line('hris_attendance_approval') . ' ' . $this->lang->line('hris_date')
								, $this->access_rights['DELETE'] ? '&nbsp;' : NULL
							)
						, 'FIELDS' => 'id, `fn_hris_master_data_get_name`(37, request_type__id, "'.$this->session->userdata('session_user_language_id').'") as request_type_name, number, date'
								. ', date_start, date_end, duration'
								. ', `fn_hris_master_data_get_name`(44, request_approval_status__id, "'.$this->session->userdata('session_user_language_id').'") as request_approval_status_name, approval_date'
						, 'FIELDS_EDIT' => 'number'
						, 'FIELDS_ORDER' => 'request_approval_status__id, request_type__id, date desc'
#						, 'TOT_ROW' => $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_VIEW_TOT_ROW']
						, 'TABLE' => array
							(
								'WHERE' => array('data__id' => $this->session->userdata('session_user_id'))
							)
					)
				, 'FORM' => array
					(
						'FIELDS' => 'id, name_official'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 2
					)
				, 'FORM_MODAL' => array
					(
						'HEADING' => $this->lang->line('hris_modal')
						, 'FIELDS' => '@_id := id as id'
							. ', request_type__id'
							. ', concat("ajax_call::", "'.$this->bz_segment['SITE_URL'].'", "::ajx_live_::combo_put::", "employee_leave_management/72/", request_type__id, "/::", leave_management__id) as leave_management_id'
							. ', number, date'
							. ', date_start, (select ifnull(date_format(time_start, "%H:%i"), "00:00") from bz_hris_attendance_request_permit where request__id = @_id) as time_start'
							. ', date_end, (select ifnull(date_format(time_end, "%H:%i"), "00:00") from bz_hris_attendance_request_permit where request__id = @_id) as time_end'
							. ', reason'
							. ', (select ifnull(responsibility, "") from bz_hris_attendance_request_leave where request__id = @_id) as responsibility'
							. ', (select ifnull(contact_information, "") from bz_hris_attendance_request_leave where request__id = @_id) as contact_information'
							. ', (select ifnull(case hospitalization_status when "Yes" then "'.$this->lang->line('hris_yes').'" else "'.$this->lang->line('hris_no').'" end, "") from bz_hris_attendance_request_sick where request__id = @_id) as hospitalization_status'
							. ', (select ifnull(case letter_status when "Yes" then "'.$this->lang->line('hris_yes').'" else "'.$this->lang->line('hris_no').'" end, "") from bz_hris_attendance_request_sick where request__id = @_id) as letter_status'
							. ', (select case letter_date when "0000-00-00" then "" else letter_date end from bz_hris_attendance_request_sick where request__id = @_id) as letter_date'
							. ', (select ifnull(medical_disease__id, "") from bz_hris_attendance_request_sick where request__id = @_id) as medical_desease_id'
							. ', request_approval_status__id'
							. ', concat("ajax_call::", "'.$this->bz_segment['SITE_URL'].'", "::ajx_live_::remaining_leave_put::", leave_management__id, "::") as remaining_leave'
						, 'FIELDS_ORDER' => ''
						, 'COLUMN' => 4
						, 'COLUMN_JOIN' => 2
					)
				, 'URL' => array
					(
						'FORM' => $this->bz_segment['SITE_URL'].'/form'
						, 'VIEW' => $this->bz_segment['SITE_URL'].'/view'
					)
			);

		$this->tbl_viewdetail_attr = array
			(
				'NAME' => $this->tbl_attr['TABLE_MODAL']['NAME_ALIAS']
				, 'PRIMARY' => $this->tbl_attr['TABLE_MODAL']['PRIMARY']
				, 'HEADING' => $this->tbl_attr['VIEW_DETAIL']['HEADING']
				, 'FIELDS' => array
					(
						'NAME' => $this->tbl_attr['VIEW_DETAIL']['FIELDS']
						, 'CAPTION' => $this->tbl_attr['VIEW_DETAIL']['FIELDS_EDIT']
						, 'ORDER' => $this->tbl_attr['VIEW_DETAIL']['FIELDS_ORDER']
					)
				, 'JAVASCRIPT' => array
					(
						'VIEWLIST_GENERATE' => array
							(
								'JS_FUNCTION_NAME' => 'js_modaldialog_data_put'
								, 'JS_FUNCTION_PARAMETERS' => '"'.$this->bz_segment['SITE_URL'].'/ajx_live_form_detail", "frm_modal", "btn_search", "txt71", "js_modal_functions", "'.$this->bz_segment['SITE_URL'].'", "ajx_modal_data_", "put"'
							)
					)
			);

		/**
		 * @description template lite variables
		 */
		$this->bz_tlite_var['CACHE_PATH'] = $this->bz_tlite_var['SESSION']['USER_ID'].'|'.str_replace('/', '|', $this->bz_segment['URL']);
		$this->bz_tlite_var['CACHE'] = array
			(
				'PUBLIC' => $this->bz_tlite_var['SESSION']['USER_ID']
				, 'PRIVATE' => array
					(
						'GLOBAL' => $this->bz_tlite_var['CACHE_PATH']
						, 'VIEW' => $this->bz_tlite_var['CACHE_PATH'].'|view'
						, 'FORM' => $this->bz_tlite_var['CACHE_PATH'].'|form'
					)
			);
	}


	/**
	 * @function index()
	 * @description main controller
	 */
	public function index()
	{
		/**
		 * @description choose which controller to show
		 */
		redirect($this->tbl_attr['URL']['FORM']);
	}


	/**
	 * @function form()
	 * @description form for data entry
	 */
	public function form()
	{
		if ($this->web_data['BISKITZ']['HEADER']['BROWSER_SUPPORTED'] == '')
		{
			/**
			 * @description if the user has ADD or EDIT access, show the form!
			 */
			if ($this->access_rights['ADD'] OR $this->access_rights['EDIT'])
			{
				/**
				 * @description if there isn't any css/ajax cache, build it! else show cache!
				 */
				if ( ! $this->bz_tlite->is_cached($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']))
				{
					/**
					 * @section begin page init
					 */
						/**
						 * @description show the menu
						 */
						$menu = $this->M_menu->shows();
						$this->web_data['JAVASCRIPT_MENU'] = $menu['JAVASCRIPT_MENU'];
						$this->web_data['PAGE']['MENU'] = $menu['PAGE']['MENU'];

						/**
						 * @description init the css and the ajax needed
						 */
						$css_files = array
							(
								array('layout' => 'screen')
								, array('tipsy/tipsy' => 'screen')
								, array('validate/validate' => 'screen')
								, array('form_icon_bar' => 'screen')
								, array('nanoscroller/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('shortcut_bar' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? 'screen' : NULL)
								, array('jqModal/jqModal' => 'screen')
								, array('dynDateTime/dynDateTime' => 'screen')
								, array('clockpick/clockpick' => 'screen')
								, array('ddm/multi-ddm' => 'screen')
							);
						$js_files = array
							(
								'js/plugins/ddm' => array('jquery.multi-ddm.pack')
								, 'js/plugins/nanoscroller' => ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error') ? array('jquery.nanoscroller.min', 'overthrow.min') : NULL
								, 'js/plugins/validate' => array('jquery.validate.pack', 'additional-methods', 'jquery.metadata')
								, 'js/plugins/tipsy' => array('jquery.tipsy.pack')
								, 'js/plugins/jqModal' => array('jqModal.pack')
								, 'js/plugins/date' => array('date')
								, 'js/plugins/dynDateTime' => array('jquery.dynDateTime.pack')
								, 'js/plugins/dynDateTime/lang/pack' => array('calendar-'.$this->session->userdata('session_user_language_abbr'))
								, 'js/plugins/clockpick' => array('jquery.clockpick.min')
								, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_FUNCTIONS_PATH'] => array('common', 'form', 'error', 'modaldialog', 'datetime_picker')
								, 'themes/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN'].'/'.$this->web_data['BISKITZ']['SETTINGS']['BISKITZ_JS_PATH'] => array('common')
							);
					/**
					 * @section end page init
					 */


					/**
					 * @section begin form initialize
					 */
						$shortcut_f8_action = '$(\'#btn_add_detail\').click();';

						/**
						 * @description initialize the form elements
						 */
						$form_content = array
							(
								'<b>'.$this->lang->line('hris_period').'</b>'
								, form_input
									(
										array
										(
											'name' => 'txtinput[0]', 'id' => 'txt0'
											, 'size' => '11', 'maxlength' => '10'
											, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
										)
										, ''
										, 'onBlur="javascript: calendar.hide();"'
									)
									. '&nbsp;' . $this->lang->line('hris_to') . '&nbsp;'
									. form_input
										(
											array
											(
												'name' => 'txtinput[1]', 'id' => 'txt1'
												, 'size' => '11', 'maxlength' => '10'
												, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
											)
											, ''
											, 'onBlur="javascript: calendar.hide();"'
										)


									. form_submit
										(
											array('id' => 'btn_search', 'class' => 'input_not_default input_no_margin')
											, $this->lang->line('button_search')
										)
									. (
											$this->access_rights['ADD']
											? form_button
												(
													array('id' => 'btn_add_detail', 'class' => 'input_default hidden')
													, $this->lang->line('button_add_detail')
												)
											: NULL
										)
							);

						/**
						 * @description create the form and elements
						 */
						$this->table->clear();
						$table_template = array('table_open' => '<table class = "plain">');
						$this->table->set_template($table_template);

						$form_content = $this->table->make_columns($form_content, $this->tbl_attr['FORM']['COLUMN']);
						$form_content = $this->table->generate($form_content);
					/**
					 * @section end form initialize
					 */



					/**
					 * @section begin generate template variables
					 */
						/**
						 * @description css and ajax
						 */
						$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
						$this->web_data['PAGE']['JAVASCRIPT_FILES'] = $this->M_template->js_generate($js_files);

						/**
						 * @description initialize the additional javascript
						 */
						$this->web_data['JAVASCRIPT'] .= '
							<script>
								var js_modal_functions =
								"
									row_leave_type = $(\"#table_modaldialog tr:eq(1), #table_modaldialog tr:eq(2)\");
									row_leave = $(\"#table_modaldialog tr:eq(6), #table_modaldialog tr:eq(7)\");
									row_permit = $(\"#table_modaldialog tr:eq(4) .input_clockpick\");
									row_sick = $(\"#table_modaldialog tr:eq(8), #table_modaldialog tr:eq(9), #table_modaldialog tr:eq(10)\");
										row_leave.hide();
										row_permit.hide();
										row_sick.hide();


									if ($(\"#txt71\").val() != \"\")
									{
										switch ($(\"#txt71\").val())
										{
											case \"217\":
												row_leave_type.show();
												row_leave.show();
												row_permit.hide();
												row_sick.hide();
												leave_code = \"LVR\";
												break;

											case \"218\":
												row_leave_type.show();
												row_leave.hide();
												row_permit.show();
												row_sick.hide();
												leave_code = \"PMR\";
												break;

											case \"261\":
												row_leave_type.hide();
												row_leave.hide();
												row_permit.hide();
												row_sick.show();
												leave_code = \"SLR\";
												break;

											default:
												row_leave_type.show();
												row_leave.hide();
												row_permit.hide();
												row_sick.hide();
												break;
										}
									}

									switch ($(\"#txt86\").val())
									{
										case \"\":
										case \"262\":
											js_datetime_picker_show(\"txt75\", \"txt76\");
											js_datetime_picker_show(\"txt77\", \"txt78\");
											js_datetime_picker_show(\"txt84\", \"txt84\");

											$(\"#txt74, #txt75, #txt77, #txt84\").val(Date.today().toString(\"yyyy-MM-dd\"));
											$(\"#txt76, #txt78\").clockpick
												(
													{
														starthour: 0
														, endhour: 23
														, minutedivisions: 4
														, military: true
													}
												);
											break;

										default:
											$(\":input\", \"#frm_modal\").each
												(function()
												{
													object_id = $(this).attr(\"id\");
													if (object_id.substring(0, 3) == \"txt\" && $(this).is(\":visible\"))
													{
														object_type = this.tagName.toLowerCase();
														switch (object_id)
														{
															case \"txt72\":
																$(this).attr(\"disabled\", \"disabled\").addClass(\"read_only select_remove_arrow\");
																break;

															case \"txt73\":
																$(this).replaceWith(\"<div id=\'\" + object_id + \"\' class=\'bold\'>\" + $(this).val() + \"</div>\");
																break;

															default:
																if (object_type == \"select\")
																{
																	$(this).replaceWith(\"<div id=\'\" + object_id + \"\'>\" + $(\"#\" + object_id + \" :selected\").text() + \"</div>\");
																}
																else
																{
																	$(this).replaceWith(\"<div id=\'\" + object_id + \"\'>\" + $(this).val() + \"</div>\");
																}
																break;
														}
													}
													$(\"#table_modaldialog tr:last\").hide();
												});
											break;
									}


									$(\"#txt71\").change
										(function()
										{
											leave_code = \"\";
											$(\"#txt76, #txt78\").val(\"00:00\");
											$(\"#txt85\").val(\"\");

											if ($(this).val() != \"\")
											{
												switch ($(this).val())
												{
													case \"217\":
														row_leave_type.show();
														row_leave.show();
														row_permit.hide();
														row_sick.hide();
														leave_code = \"LVR\";
														break;

													case \"218\":
														row_leave_type.show();
														row_leave.hide();
														row_permit.show();
														row_sick.hide();
														leave_code = \"PMR\";
														break;

													case \"261\":
														row_leave_type.hide();
														row_leave.hide();
														row_permit.hide();
														row_sick.show();
														leave_code = \"SLR\";
														break;

													default:
														row_leave_type.show();
														row_leave.hide();
														row_permit.hide();
														row_sick.hide();
														break;
												}

												if (leave_code != \"\")
												{
													js_form_action
													(
														\"'.$this->bz_segment['SITE_URL'].'\", \"ajx_live_\", \"combo_put\", \"employee_leave_management/72/\" + $(this).val()
														, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
													);

													js_form_action
													(
														\"'.$this->bz_segment['SITE_URL'].'\", \"ajx_live_\", \"request_number_put\", leave_code
														, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
													);
												}
												else
												{
													$(\"#txt73\").val(\"\");
												}
											}
											else
											{
												$(\"#txt73\").val(\"\");
											}
										});

									$(\"#txt72\").change
										(function()
										{
											if ($(this).val() != \"\")
											{
												js_form_action
												(
													\"'.$this->bz_segment['SITE_URL'].'\", \"ajx_live_\", \"remaining_leave_put\", $(this).val()
													, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
												);


												if ($(\"#txt71\").val() != \"\")
												{
													switch ($(\"#txt71\").val())
													{
														case \"218\":
															day_type_time = $(\"#txt76, #txt78\").val(\"00:00\");
															if ($(\"#txt72 :selected\").text().search(/(Part of Day)/i) > 0)
															{
																day_type_time.show();
															}
															else
															{
																day_type_time.hide();
															}
															break;
													}
												}

												switch ($(this).val())
												{
													case \"2\":
														$(\".hris_to, #txt77\").addClass(\"hidden\");
														break;

													default:
														$(\".hris_to, #txt77\").removeClass(\"hidden\");
														break;
												}
											}
											else
											{
												$(\"#txt87, #txt88\").text(\"\");
											}
										});

									$(\"#txt75\").blur
										(function()
										{
											switch ($(\"#txt72\").val())
											{
												case \"2\":
													/* Maternity Leave */
													$(\"#txt77\").val(Date.parse($(\"#txt75\").val()).addMonths(parseInt($(\"#txt87\").text())).toString(\"yyyy-MM-dd\"));
													break;
											}
										});

									$(\"#txt83\").change
										(function()
										{
											if ($(this).val() == \"No\")
											{
												$(\"#txt84\").val(\"0000-00-00\").addClass(\"hidden\");
											}
											else
											{
												$(\"#txt84\").val(Date.today().toString(\"yyyy-MM-dd\")).removeClass(\"hidden\");
											}
										});


									$(\"#btn_modal_save\").click
										(function()
										{
											js_form_action_validate
											(
												\"'.$this->bz_segment['SITE_URL'].'\", \"ajx_modal_\", \"insert\", $(\"#txt0\").val() + \"/\" + $(\"#txt1\").val()
												, \"#frm_modal\", \"#process_modaldialog\", \"#error_message_modaldialog\"
												, \"validate\"
											);

											$(\"#txt71\").focus().select();
										});
								";

								var js_modaldialog_popup =
								"
									js_modaldialog_show
									(
										\"'.$this->bz_segment['SITE_URL'].'/ajx_live_form_detail/\"
										, \"frm_modal\"
										, \"btn_search\", \"txt71\"
										, js_modal_functions
									);
								";

								var js_data_put =
								"
									js_form_action
									(
										\"' . $this->bz_segment['SITE_URL'] . '\", \"ajx_data_\", \"put\", \"\"
										, \"#frm_header\", \"#process\", \"#error_message\"
									);
								";


								$(function()
								{
									$("#btn_add_detail").click
										(function()
										{
											eval(js_modaldialog_popup);
										});

									$("#btn_search").click
										(function()
										{
											eval(js_data_put);
										});

									js_datetime_picker_show("txt0", "txt1");
									js_datetime_picker_show("txt1", "btn_search");

									$("#txt0").val(Date.today().toString("yyyy-MM-01"));
									$("#txt1").val(Date.today().toString("yyyy-MM-dd"));

									$("#btn_search").focus().select();

									eval(js_data_put);
						';

						if ($menu['PAGE']['MENU']['FAVORITE']['LIST'] != 'Error')
						{
							$this->web_data['JAVASCRIPT'] .= '$(".nano").nanoScroller({preventPageScrolling: true});';
						}

						$this->web_data['JAVASCRIPT'] .= '
								});
							</script>
						';

						/**
						 * @description menu descriptions
						 */
						$this->web_data['PAGE']['DETAIL'] = $this->M_menu->detail();

						/**
						 * @description icon bar
						 */
						$this->load->model('admin/M_command_bar');
						$this->web_data['PAGE']['ICON_BAR'] = $this->M_command_bar->form('TRUE', FALSE, FALSE, TRUE, FALSE, FALSE);

						/**
						 * @description form dialogs
						 */
						$this->web_data['PAGE']['FORM'] = array
							(
								'OPEN' => form_open('', array('id' => 'frm_header', 'onSubmit' => 'return false'), '')
								, 'CONTENT' => $form_content
							);

						/**
						 * @description notes
						 */
						$this->web_data['PAGE']['NOTES'] = array
							(
								'USE' => 'TRUE'
								, 'MESSAGE' => array
									(
										'SHORTCUTS' => $this->lang->line('notes_shortcuts')
										, 'ADDITIONAL' => $this->lang->line('notes_additional_modaldialog')
									)
							);
					/**
					 * @section end generate template variables
					 */
				}


				/**
				 * @description log the action!
				 */
				$this->M_log->write('db_default', sprintf($this->lang->line('log_form_access'), $this->router->class, 'form'));
			}
			else
			{
				/**
				 * @description doesn't have ADD or EDIT access? redirect to desktop
				 */
				redirect('admin/desktop');
			}



		}
		else
		{
			/**
			 * @section Browser NOT Supported
			 */
				/**
				 * @description browser not supported message error
				 */
				$css_files = array(array('layout' => 'screen'));


				/**
				 * @section begin generate template variables
				 */
					/**
					 * @description css and ajax
					 */
					$this->web_data['PAGE']['CSS_FILES'] = $this->M_template->css_generate($css_files, $this->web_data['BISKITZ']['SETTINGS']['BISKITZ_THEME_ADMIN']);
				/**
				 * @section end generate template variables
				 */
		}



		/**
		 * @description output template variables
		 */
		$this->bz_tlite->assign('WEB_DATA', $this->web_data);


		/**
		 * @section begin output
		 */
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['HEADER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CSS'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['MENU'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_DESCRIPTION'], $this->bz_tlite_var['CACHE']['PRIVATE']['GLOBAL']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM_ICON_BAR'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_FORM'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['CONTENT_SHORTCUT_BAR'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT'], $this->bz_tlite_var['CACHE']['PRIVATE']['FORM']);

		$this->bz_tlite->cache = FALSE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['JAVASCRIPT_EXTEND']);

		$this->bz_tlite->cache = TRUE;
		$this->bz_tlite->display($this->bz_tlite_var['NAMES']['FOOTER_END'], $this->bz_tlite_var['CACHE']['PUBLIC']);
		/**
		 * @section end output
		 */
	}



/**
 * @section AJAX Action Section
 */

	/**
	 * @function ajx_data_put()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_data_put()
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'])
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');
			/**
			 * @description load the helper
			 */
			$this->load->helper('viewlist');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, $this->tbl_attr['TABLE']['NAME']
					, array('id' => $this->session->userdata('session_user_id'))
					, '"' . $this->input->post('txtinput[0]') . '", "' . $this->input->post('txtinput[1]') . '"'
					, 0, 'log_form_edit', 'form'

					, 'js_action_data_detail'
					, array
						(
							'FUNCTION_NAME' => 'detail_generate'
							, 'FUNCTION_ATTR' => array
								(
									'db_default'
									, $this->tbl_viewdetail_attr
									, 'data__id = "' . $this->session->userdata('session_user_id') . '" and `date` between "' . $this->input->post('txtinput[0]') . '" and "' . $this->input->post('txtinput[1]') . '"'
									, $this->tbl_viewdetail_attr['JAVASCRIPT']['VIEWLIST_GENERATE']
								)
						)
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX MODAL Action Section
 */

	/**
	 * @function ajx_modal_data_put()
	 * @description get the data from database and put them to the form via ajax!
	 */
	public function ajx_modal_data_put($prm_request_id = '')
	{
		if ($this->input->is_ajax_request() && $this->access_rights['EDIT'] && trim($prm_request_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, $this->tbl_attr['TABLE_MODAL']['NAME_ALIAS']
					, array('id' => $prm_request_id, 'data__id' => $this->session->userdata('session_user_id'))
					, $this->tbl_attr['FORM_MODAL']['FIELDS']
					, 70
					, 'log_form_edit'
					, 'modal'
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_modal_insert()
	 * @description insert the detail data
	 */
	public function ajx_modal_insert($prm_period_start = '', $prm_period_end = '')
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']) && trim($prm_period_start) != '' && trim($prm_period_end) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');
			/**
			 * @description load the helper
			 */
			$this->load->helper('viewlist');


			/**
			 * @section rules checking
			 */
			/**
			 * @description duration check
			 */
			$check_duration = $this->ajx_live_check_duration($this->input->post('txtinput[71]'), $this->input->post('txtinput[72]'));
			switch ($check_duration)
			{
				case 'OK':
					/**
					 * @description range date check
					 */
					if ($this->ajx_live_check_date_range($this->input->post('txtinput[75]'), $this->input->post('txtinput[77]')) == 'OK')
					{
						/**
						 * @description insert the main table
						 */
						$data = array
							(
								'request_type__id' => $this->input->post('txtinput[71]')
								, 'leave_management__id' => trim($this->input->post('txtinput[72]')) == '' ? NULL : $this->input->post('txtinput[72]')
								, 'number' => $this->input->post('txtinput[73]')
								, 'date' => date('Y-m-d')
								, 'data__id' => $this->session->userdata('session_user_id')
								, 'date_start' => $this->input->post('txtinput[75]')
								, 'date_end' => $this->input->post('txtinput[77]')
								, 'reason' => $this->input->post('txtinput[79]')
								, 'request_approval_status__id' => 262
							);
						$return_value = $this->M_live_data->insert
							(
								'db_default'
								, $this->tbl_attr['TABLE_MODAL']['NAME']
								, array('id' => $this->input->post('txtinput[70]'))
								, $data
								, 'modal'
								, TRUE, ''
								, FALSE, FALSE
							);

						if ($return_value[0] == 'js_action_update')
						{
							$return_value = array_merge($return_value, array($this->access_rights));
						}

						/**
						 * @description insert the extended table
						 */
						$header['ID'] = $this->M_database->max_id('db_default', $this->tbl_attr['TABLE_MODAL']['NAME'], 'id');


						switch ($this->input->post('txtinput[71]'))
						{
							case '217':
								$table['NAME'] = $this->tbl_attr['TABLE_MODAL']['NAME'].'_leave';
								$data = array
									(
										'request__id' => $header['ID']
										, 'responsibility' => $this->input->post('txtinput[80]')
										, 'contact_information' => $this->input->post('txtinput[81]')
									);
								break;

							case '218':
								$table['NAME'] = $this->tbl_attr['TABLE_MODAL']['NAME'].'_permit';
								$data = array
									(
										'request__id' => $header['ID']
										, 'time_start' => $this->input->post('txtinput[76]')
										, 'time_end' => $this->input->post('txtinput[78]')
									);
								break;

							case '261':
								$table['NAME'] = $this->tbl_attr['TABLE_MODAL']['NAME'].'_sick';
								$data = array
									(
										'request__id' => $header['ID']
										, 'hospitalization_status' => $this->input->post('txtinput[82]')
										, 'letter_status' => $this->input->post('txtinput[83]')
										, 'letter_date' => $this->input->post('txtinput[84]')
										, 'medical_disease__id' => $this->input->post('txtinput[85]')
									);
								break;
						}

						$this->M_live_data->insert
							(
								'db_default'
								, $table['NAME']
								, array('request__id' => $header['ID'])
								, $data
								, 'modal'
								, TRUE, ''
								, FALSE, FALSE
							);


						$modaldialog_status = 'modaldialog_close_auto';


						/**
						 * @description generate the viewlist live!
						 */
						$return_value = array
							(
								'js_action_modal_insert'
								, $return_value[1], array($header['ID'])

								/**
								 * @description the header id in the MODAL form, use it to sync with the main form
								 */
								, 70

								/**
								 * @description put the header data to main form after success inserting detail data
								 */
								, array('', '')

								/**
								 * @description generate the detail view list, LIVE!
								 */
								, array
									(
										'content_replace'
										, detail_generate
											(
												'db_default'
												, $this->tbl_viewdetail_attr
												, 'data__id = "' . $this->session->userdata('session_user_id') . '" and `date` between "' . $prm_period_start . '" and "' . $prm_period_end . '"'
												, $this->tbl_viewdetail_attr['JAVASCRIPT']['VIEWLIST_GENERATE']
											)
									)

								/**
								 * @description trigger the dialog to be automatically closed
								 */
								, array($modaldialog_status)
							);

					}
					else
					{
						$return_value = array
							(
								'js_action_message_not_clear'
								, array($this->lang->line('hris_msg_request_check_date_range_already_exist'), 'error')
							);
					}
					break;


				case 'ERROR':
					$return_value = array
						(
							'js_action_message_not_clear'
							, array($this->lang->line('hris_msg_request_check_duration_over_quota'), 'error')
						);
					break;


				case 'DURATION_IS_ZERO':
					$return_value = array
						(
							'js_action_message_not_clear'
							, array($this->lang->line('hris_msg_request_check_duration_is_zero'), 'error')
						);
					break;
			}


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}



/**
 * @section AJAX Live Section
 */

	/**
	 * @function ajx_live_form_detail
	 * @description generate the detail form via ajax
	 */
	public function ajx_live_form_detail()
	{
		if ($this->input->is_ajax_request() && ($this->access_rights['ADD'] OR $this->access_rights['EDIT']))
		{
			/**
			 * @description load the models
			 */
			$this->load->model(array('admin/M_layout', 'admin/hris/HRIS_M_live_data'));

			/**
			 * @section begin form MODAL initalize
			 */
				/**
				 * @description create the MODAL form and elements
				 */
				$form_modal_content_join[] = array
					(
						'<b>'.$this->lang->line('hris_type').'</b>'
						, array
							(
								'data' => form_input
									(
										array
										(
											'type' => 'hidden'
											, 'name' => 'txtinput[70]', 'id' => 'txt70'
											, 'size' => '18', 'maxlength' => '18'
											, 'readOnly' => 'readOnly', 'class' => 'read_only'
										)
									)
									. form_dropdown
										(
											'txtinput[71]'
											, $this->HRIS_M_live_data->combo_fill('db_default', 37)
											, ''
											, 'id="txt71" class="{validate: {required: true}}"'
										)
								, 'colspan' => 3
							)

						, '<b>'.$this->lang->line('hris_attendance_leave_leave').'</b>'
						, array
							(
								'data' => form_dropdown
									(
										'txtinput[72]'
										, array('' => $this->lang->line('hris_choose'))
										, ''
#													, 'id="txt2" class="{validate: {required: true}}"'
										, 'id="txt72"'
									)
								, 'colspan' => 3
							)

						, '<b>'.$this->lang->line('hris_remaining').'</b>'
						, array
							(
								'data' => '<span id="txt87"></span>&nbsp;<span id="txt88"></span>'
								, 'colspan' => 3
							)
					);

				$form_modal_content[] = array
					(
						'<b>'.$this->lang->line('hris_number').'</b>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[73]', 'id' => 'txt73'
									, 'size' => '22', 'maxlength' => '20'
									, 'readOnly' => 'readOnly' , 'class' => 'read_only bold focus_no'
								)
							)

						, '<b>'.$this->lang->line('hris_date').'</b>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[74]', 'id' => 'txt74'
									, 'size' => '11', 'maxlength' => '10'
									, 'readOnly' => 'readOnly' , 'class' => 'read_only focus_no'
								)
							)

						, '<b>'.$this->lang->line('hris_from').'</b>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[75]', 'id' => 'txt75'
									, 'size' => '11', 'maxlength' => '10'
									, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
								)
								, ''
								, 'onBlur="javascript: calendar.hide();"'
							)
							. form_input
								(
									array
									(
										'name' => 'txtinput[76]', 'id' => 'txt76'
										, 'size' => '6', 'maxlength' => '5'
										, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_clockpick'
									)
									, '00:00'
								)

						, '<b class="hris_to">'.$this->lang->line('hris_to').'</b>'
						, form_input
							(
								array
								(
									'name' => 'txtinput[77]', 'id' => 'txt77'
									, 'size' => '11', 'maxlength' => '10'
									, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
								)
								, ''
								, 'onBlur="javascript: calendar.hide();"'
							)
							. form_input
								(
									array
									(
										'name' => 'txtinput[78]', 'id' => 'txt78'
										, 'size' => '6', 'maxlength' => '5'
										, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_clockpick'
									)
									, '00:00'
								)
					);

				$form_modal_content_join[] = array
					(
						array('data' => '<b>'.$this->lang->line('hris_reason').'</b>', 'valign' => 'top')
						, array
							(
								'data' => form_textarea
									(
										array
										(
											'name' => 'txtinput[79]', 'id' => 'txt79'
											, 'cols' => '60', 'rows' => '3'
											, 'style' => 'width: 100%'
										)
									)
								, 'colspan' => 3
							)

						, array('data' => '<b>'.$this->lang->line('hris_attendance_request_leave_responsibility').'</b>', 'valign' => 'top')
						, array
							(
								'data' => form_textarea
									(
										array
										(
											'name' => 'txtinput[80]', 'id' => 'txt80'
											, 'cols' => '60', 'rows' => '2'
											, 'style' => 'width: 100%'
										)
									)
								, 'colspan' => 3
							)

						, array('data' => '<b>'.$this->lang->line('hris_attendance_request_leave_contact_information').'</b>', 'valign' => 'top')
						, array
							(
								'data' => form_textarea
									(
										array
										(
											'name' => 'txtinput[81]', 'id' => 'txt81'
											, 'cols' => '60', 'rows' => '2'
											, 'style' => 'width: 100%'
										)
									)
								, 'colspan' => 3
							)

						, '<b>'.$this->lang->line('hris_attendance_request_sick_hospitalization').'</b>'
						, array
							(
								'data' => form_dropdown
									(
										'txtinput[82]'
										, array
											(
												'Yes' => $this->lang->line('hris_yes')
												, 'No' => $this->lang->line('hris_no')
											)
										, ''
										, 'id="txt82" class="{validate: {required: true}}"'
									)
								, 'colspan' => 3
							)

						, '<b>'.$this->lang->line('hris_letter').' '.$this->lang->line('hris_status').'</b>'
						, array
							(
								'data' => form_dropdown
									(
										'txtinput[83]'
										, array
											(
												'Yes' => $this->lang->line('hris_yes')
												, 'No' => $this->lang->line('hris_no')
											)
										, ''
										, 'id="txt83" class="{validate: {required: true}}"'
									)
									. form_input
										(
											array
											(
												'name' => 'txtinput[84]', 'id' => 'txt84'
												, 'size' => '11', 'maxlength' => '10'
												, 'readOnly' => 'readOnly', 'class' => '{validate: {required: true}} input_calendar do_not_change'
											)
											, ''
											, 'onBlur="javascript: calendar.hide();"'
										)
								, 'colspan' => 3
							)

						, '<b>'.$this->lang->line('hris_desease').'</b>'
						, array
							(
								'data' => form_dropdown
									(
										'txtinput[85]'
										, $this->HRIS_M_live_data->combo_fill('db_default', 15)
										, ''
										, 'id="txt85"'
									)
								, 'colspan' => 3
							)


						, '&nbsp;'
						, array
							(
								'data' => form_input
									(
										array
										(
											'type' => 'hidden'
											, 'name' => 'txtinput[86]', 'id' => 'txt86'
											, 'size' => '18', 'maxlength' => '18'
											, 'readOnly' => 'readOnly', 'class' => 'read_only'
										)
									)
									. form_submit
										(
											array('id' => 'btn_modal_save')
											, $this->lang->line('button_save')
										)
								, 'colspan' => 3
							)
					);
				/**
				 * @description create the form MODAL and elements
				 */
				$this->table->clear();
				$table_template = array('table_open' => '<table id = "table_modaldialog">');
				$this->table->set_template($table_template);

				$form[] = $this->table->make_columns($form_modal_content_join[0], $this->tbl_attr['FORM_MODAL']['COLUMN_JOIN']);
				$form[] = $this->table->make_columns($form_modal_content[0], $this->tbl_attr['FORM_MODAL']['COLUMN']);
				$form[] = $this->table->make_columns($form_modal_content_join[1], $this->tbl_attr['FORM_MODAL']['COLUMN_JOIN']);

				$form_modal_content = $this->table->generate(array_merge($form[0], $form[1], $form[2]));
			/**
			 * @section end form MODAL initalize
			 */


			/**
			 * @section begin generate template variables
			 */
			$return_value = array
				(
					/**
					 * @description form modal dialogs
					 */
					'FORM_MODAL' => array
						(
							'OPEN' => form_open('', array('id' => 'frm_modal', 'onSubmit' => 'return false'), '')
							, 'TITLE' => $this->tbl_attr['FORM_MODAL']['HEADING']
							, 'CONTENT' => $form_modal_content
						)

					/**
					 * @description notes modaldialog
					 */
					, 'NOTES_MODAL' => array
						(
							'USE' => 'TRUE'
							, 'MESSAGE' => array
								(
									'SHORTCUTS' => $this->lang->line('notes_modaldialog_shortcuts')
									, 'ADDITIONAL' => ''
								)
						)
				);
			/**
			 * @section end generate template variables
			 */


			$return_value = $this->M_layout->modaldialog_generate($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_request_number_put
	 * @description put the auto generated request number
	 */
	public function ajx_live_request_number_put($prm_code = '', $prm_object_start = '73')
	{
		if ($this->input->is_ajax_request())
		{
			$this->db_default->select('`fn_hris_attendance_request_number_generate`("'.$prm_code.'") as request_number', FALSE);
			$query = $this->db_default->get();

			$return_value = array
				(
					'js_action_data'
					, array($query->row()->request_number)
					, $prm_object_start
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_remaining_leave_put
	 * @description put the remaining leave based on the leave type
	 */
	public function ajx_live_remaining_leave_put($prm_leave_management_id = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_leave_management_id) != '')
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/M_live_data');


			$return_value = $this->M_live_data->put
				(
					'db_default'
					, 'hris_employee_leave_management'
					, array('data__id' => $this->session->userdata('session_user_id'), 'leave_management__id' => $prm_leave_management_id, 'activated' => 'Yes')
					, 'total, case leave_management__id when 2 then "'.$this->lang->line('hris_months').'" else "'.$this->lang->line('hris_days').'" end as total_type'
					, 87
				);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_combo_put
	 * @description get the data and put them in combobox
	 */
	public function ajx_live_combo_put($prm_module_table = 'employee_leave_management', $prm_object_start = '72', $prm_where_key = '', $prm_selected_value = '')
	{
		if ($this->input->is_ajax_request())
		{
			/**
			 * @description load the models
			 */
			$this->load->model('admin/hris/HRIS_M_live_data');


			$table['TYPE'] = 'NORMAL';
			$table['CHOOSE_SHOW'] = TRUE;
			$table['QUERY_SPECIAL'] = FALSE;
			switch ($prm_module_table)
			{
				case 'employee_leave_management':
					$table['NAME'] = 'hris_employee_leave_management';
					switch ($prm_where_key)
					{
						case '261':
							/**
							 * @description sick leave type
							 */
							$table['WHERE'] = array('data__id' => '"'.$this->session->userdata('session_user_id').'"', 'activated' => '"Yes"', 'leave_management__id' => 0);
							$table['CHOOSE_SHOW'] = FALSE;
							break;

						default:
							$table['WHERE'] = array('data__id' => '"'.$this->session->userdata('session_user_id').'"', 'activated' => '"Yes"', 'leave_management__id in ' => '(select id from bz_hris_attendance_leave_management where request_type__id = '.$prm_where_key.')');
							break;
					}
					$table['QUERY_SPECIAL'] = TRUE;
					break;
			}


			$return_value = $this->HRIS_M_live_data->combo_fill('db_default', $table['NAME'], $prm_object_start, $table['TYPE'], $table['WHERE'], FALSE, $prm_selected_value, $table['CHOOSE_SHOW'], $table['QUERY_SPECIAL']);


			$return_value = json_encode($return_value);
			echo $return_value;
		}
	}


	/**
	 * @function ajx_live_check_duration
	 * @description check date duration and compare with the total of the specific leave_management__id
	 */
	public function ajx_live_check_duration($prm_request_type_id = '', $prm_leave_management_id = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_request_type_id) != '')
		{
			switch ($prm_request_type_id)
			{
				case 261:
					/**
					 * @description Sick Leave
					 */
					$return_value = 'OK';
					break;

				default:
					/**
					 * @description NON Sick Leave
					 */
					$sql = 'select `fn_hris_attendance_request_duration`(?, ?, ?, ?) as duration, total from bz_hris_employee_leave_management where data__id = ? and leave_management__id = ? and activated = ?';
					$query = $this->db_default->query($sql, array($this->session->userdata('session_user_id'), $prm_leave_management_id, $this->input->post('txtinput[75]'), $this->input->post('txtinput[77]'), $this->session->userdata('session_user_id'), $prm_leave_management_id, 'Yes'));

					$return_value = 'ERROR';
					if ($query->num_rows() > 0)
					{
						$row = $query->row();
						if ($row->duration > 0)
						{
							($row->duration <= $row->total) ? $return_value = 'OK' : $return_value = 'ERROR';
						}
						else
						{
							$return_value = 'DURATION_IS_ZERO';
						}
					}
					break;
			}


			return $return_value;
		}
	}


	/**
	 * @function ajx_live_check_date_range
	 * @description check the choosen date range
	 */
	public function ajx_live_check_date_range($prm_period_start = '', $prm_period_end = '')
	{
		if ($this->input->is_ajax_request() && trim($prm_period_start) != '' && trim($prm_period_end) != '')
		{
			$sql_store_procedure = 'select `fn_hris_attendance_request_clash_check`(?, ?, ?) as date_range_availability';
			$query_result = $this->db_default->query($sql_store_procedure, array($this->session->userdata('session_user_id'), $this->input->post('txtinput[75]'), $this->input->post('txtinput[77]')));

			$return_value = 'ERROR';
			if ($query_result->num_rows() > 0)
			{
				$row = $query_result->row();

				$return_value = $row->date_range_availability;
			}


			return $return_value;
		}
	}



}

/** End of file request.php */
/** Location: ./biskitz_mpc/biskitz/controllers/admin/hris/attendance/request.php */
